package com.gwtsz.chart.output.utils;

import java.util.ArrayList;




/**
 * Created by reeta.zou on 2016/10/13.
 * K线数据model
 */
public class GTTKDataManager {

    /**分时数据集合*/
    public ArrayList<GTTTimeDataModel> mTimeDataList;
    /**分时图请求数据时间参数*/
    public long lastTime = 0;
    public int uiStartTime = 0;

    public ArrayList<GTTKDataModel> mCacheList;
    /**主图指标对象*/
    public GTTIndicatorsModel mSuperIndicators;
    /**副图指标对象*/
    public GTTIndicatorsModel mSecondIndicators;
    /** k线总条数  */
    public int kLineNumbers = 0;
    /**服务器返回k线的次数 */
    public int reqKLineNumber = 0;
    /** 主图指标参数 **/
    public GTTFormularContent mSuperFormularContent;
    /** 副图指标参数 **/
    public GTTFormularContent mSecondFormularContent;

    /** 当屏最右时间 **/
    public int ViewIndexRightTime=0;
    /** 当屏显示数量 **/
    public int ViewNumber=400;
    /** 是否为报价更新 **/
    public boolean isUpdataFormServer=true;



    /**第一根线坐标 */
    public int firstLineIndex = 0;

    //public TimelineScale mTimelineScale = ChartConfig.instance().getChartTimelineScale();

    private static GTTKDataManager mInstance = null;

    public static GTTKDataManager instance(){
        if(mInstance == null){
            mInstance = new GTTKDataManager();
        }

        return mInstance;
    }

    private GTTKDataManager() {
        mCacheList = new ArrayList<>();
        //mSuperIndicators = new GTTIndicatorsModel();
        //mSecondIndicators = new GTTIndicatorsModel();
        mTimeDataList = new ArrayList();
        mSuperFormularContent=new GTTFormularContent();
        mSecondFormularContent=new GTTFormularContent();
    }

    public int getViewIndexRightTime() {
        return ViewIndexRightTime;
    }

    public void setViewIndexRightTime(int viewIndexRightTime) {
        ViewIndexRightTime = viewIndexRightTime;
    }

    public int getViewNumber() {
        return ViewNumber;
    }

    public void setViewNumber(int viewNumber) {
        ViewNumber = viewNumber;
    }

    public boolean isUpdataFormServer() {
        return isUpdataFormServer;
    }

    public void setUpdataFormServer(boolean updataFormServer) {
        isUpdataFormServer = updataFormServer;
    }

    /**
     * 设置k线绘制的大小规模
     * @param scale
     */
    /*public void setTimelineScale(TimelineScale scale){
        this.mTimelineScale = scale;
    }

    public int barsPerGrid(){
        return mTimelineScale.barsPerGrid();
    }*/
    /**
     * 追加k线数据
     */
    public void addAllKLineData(ArrayList<GTTKDataModel> kLineLists){
        synchronized (this) {
            mCacheList.addAll(0, kLineLists);
            reqKLineNumber++;
            kLineNumbers = mCacheList.size();
        }
    }
    /**
     * 追加一根k线数据
     */
    public void addKLineData(GTTKDataModel kDataModel){
        mCacheList.add(kDataModel);
        kLineNumbers = mCacheList.size();
    }

    /**
     * 当前最新k线是否显示在界面上
     * @return
     */
    /*public boolean hasShowLastLine(){
        return firstLineIndex + mTimelineScale.barsPerGrid() == kLineNumbers ? true:false;
    }*/

    /**
     * 返回最新k线
     * @return
     */
    public GTTKDataModel getLastLineModel(){
        if(hasKLineData()){
            return getKLineModel(getSize()-1);
        }
        return null;
    }

    /**
     * 清除k线数据数据
     */
    public void clearKData(){
        if(null != mCacheList){
            mCacheList.clear();
        }
        reqKLineNumber = 0;
        kLineNumbers = 0;
        mInstance = null;
    }

    /**
     * 清除指标数据
     */
    /*public void clearIndictor(){
        mSuperIndicators = new GTTIndicatorsModel();
        mSecondIndicators = new GTTIndicatorsModel();
    }*/

    /**
     * 清除分时数据
     */
    public void clearTimeData(){
        if(null != mTimeDataList){
            mTimeDataList.clear();
        }

        lastTime = 0;
    }



    /**
     * 返回长度
     * @return
     */
    public int getSize(){
        return mCacheList.size();
    }

    /**
     * 返回第一条数据
     * @return
     */
    public GTTKDataModel getStartData(){
        if(mCacheList.size() > 0){
            return mCacheList.get(0);
        }

        return null;
    }

    /**
     * 添加k线数据集合
     * @param mData
     */
    public void addKLineList(ArrayList<GTTKDataModel> mData){
        mCacheList.addAll(mData);
    }

    /**
     * 添加k线数据对象
     * @param mData
     */
    public void addKLineModel(GTTKDataModel mData){
        mCacheList.add(mData);
    }

    /**
     * 返回k线数据对象
     * @param position
     */
    public GTTKDataModel getKLineModel(int position){
        if(mCacheList.size() > 0 && (position >= 0 && position < mCacheList.size())){
            return mCacheList.get(position);
        }else{
            return null;
        }
    }

    /**
     * 返回k线数据对象
     */
    public GTTKDataModel getLastKLineModel(){
        if(mCacheList.size() > 0){
            return mCacheList.get(mCacheList.size() - 1);
        }else{
            return null;
        }
    }

    /**
     * 返回第一根k线的时间
     * @return
     */
    public int getStartTime(){
        if(mCacheList.size() > 0){
            GTTKDataModel model = mCacheList.get(0);
            if(null != model){
                return model.priceTime;
            }
            return 0;
        }else{
            return 0;
        }
    }
    /**
     * 刷新最后一根数据
     * @param mData
     */
    public void setLastKLineData(GTTKDataModel mData){
        mCacheList.set(mCacheList.size() - 1, mData);
    }

    /**
     * 判断是否有k线数据
     * @return
     */
    public boolean hasKLineData(){
        if(mCacheList.size() > 0){
            return true;
        }
        return false;
    }

    /**
     * 添加分时数据集合
     * @param mData
     */
    public void addTimeList(ArrayList<GTTTimeDataModel> mData){
        mTimeDataList.clear();
        mTimeDataList.addAll(mData);
    }

    /**
     * 添加分时数据对象
     * @param mData
     */
    public void addTimeModel(GTTTimeDataModel mData){
        mTimeDataList.add(mData);
    }

    /**
     * 返回分时数据对象
     * @param position
     */
    public GTTTimeDataModel getTimeModel(int position){
        return mTimeDataList.get(position);
    }

    /**
     * 返回分时数据对象
     */
    public GTTTimeDataModel getLastTimeModel(){
        if(mTimeDataList.size() > 0){
            return mTimeDataList.get(mTimeDataList.size() - 1);
        }else{
            return null;
        }
    }

    /**
     * 刷新最后一根数据
     * @param mData
     */
    public void setLastTimeData(GTTTimeDataModel mData){
        mTimeDataList.set(mTimeDataList.size() - 1, mData);
    }

    /**
     * 判断是否有分时图
     * @return
     */
    public boolean hasTimeData(){
        if(mTimeDataList.size() > 0){
            return true;
        }
        return false;
    }
    /**
     * 清除指标数据
     */
    public void clearIndictor(){
        mSuperIndicators = new GTTIndicatorsModel();
        mSecondIndicators = new GTTIndicatorsModel();
    }


}


